function _toConsumableArray(arr) {
  return (
    _arrayWithoutHoles(arr) ||
    _iterableToArray(arr) ||
    _unsupportedIterableToArray(arr) ||
    _nonIterableSpread()
  );
}
function _nonIterableSpread() {
  throw new TypeError(
    'Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _iterableToArray(iter) {
  if (
    (typeof Symbol !== 'undefined' && iter[Symbol.iterator] != null) ||
    iter['@@iterator'] != null
  )
    return Array.from(iter);
}
function _arrayWithoutHoles(arr) {
  if (Array.isArray(arr)) return _arrayLikeToArray(arr);
}
function _slicedToArray(arr, i) {
  return (
    _arrayWithHoles(arr) ||
    _iterableToArrayLimit(arr, i) ||
    _unsupportedIterableToArray(arr, i) ||
    _nonIterableRest()
  );
}
function _nonIterableRest() {
  throw new TypeError(
    'Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.',
  );
}
function _unsupportedIterableToArray(o, minLen) {
  if (!o) return;
  if (typeof o === 'string') return _arrayLikeToArray(o, minLen);
  var n = Object.prototype.toString.call(o).slice(8, -1);
  if (n === 'Object' && o.constructor) n = o.constructor.name;
  if (n === 'Map' || n === 'Set') return Array.from(o);
  if (n === 'Arguments' || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n))
    return _arrayLikeToArray(o, minLen);
}
function _arrayLikeToArray(arr, len) {
  if (len == null || len > arr.length) len = arr.length;
  for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
  return arr2;
}
function _iterableToArrayLimit(arr, i) {
  var _i =
    null == arr
      ? null
      : ('undefined' != typeof Symbol && arr[Symbol.iterator]) || arr['@@iterator'];
  if (null != _i) {
    var _s,
      _e,
      _x,
      _r,
      _arr = [],
      _n = !0,
      _d = !1;
    try {
      if (((_x = (_i = _i.call(arr)).next), 0 === i)) {
        if (Object(_i) !== _i) return;
        _n = !1;
      } else
        for (
          ;
          !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i);
          _n = !0
        );
    } catch (err) {
      (_d = !0), (_e = err);
    } finally {
      try {
        if (!_n && null != _i.return && ((_r = _i.return()), Object(_r) !== _r)) return;
      } finally {
        if (_d) throw _e;
      }
    }
    return _arr;
  }
}
function _arrayWithHoles(arr) {
  if (Array.isArray(arr)) return arr;
}
import { useRealtimeRef } from '@szzj/hooks';
import { useCallback, useEffect, useState } from 'react';
export default function useOther(_ref) {
  var _ref$mode = _ref.mode,
    mode = _ref$mode === void 0 ? 'multiple' : _ref$mode,
    options = _ref.options,
    value = _ref.value,
    onChange = _ref.onChange;
  /** 选项选中情况，不包含其他选项 */
  var _useState = useState([]),
    _useState2 = _slicedToArray(_useState, 2),
    selectedValue = _useState2[0],
    setSelectedValue = _useState2[1];
  /** 其他选项选中后的值 */
  var _useState3 = useState(undefined),
    _useState4 = _slicedToArray(_useState3, 2),
    otherValue = _useState4[0],
    setOtherValue = _useState4[1];
  /** 其他选项是否选中 */
  var _useState5 = useState(false),
    _useState6 = _slicedToArray(_useState5, 2),
    otherChecked = _useState6[0],
    setOtherChecked = _useState6[1];
  var optionsRef = useRealtimeRef(options);

  /**
   * 外部值变更时，引起内部状态值更新
   */
  useEffect(
    function () {
      if (value) {
        // @todo 校验 value 值类型与 mode 匹配
        var optionValues = optionsRef.current.map(function (option) {
          return option.value;
        });
        var newSelectedValue = [];
        var otherVal;
        (Array.isArray(value) ? value : [value]).forEach(function (val) {
          if (optionValues.includes(val)) {
            newSelectedValue.push(val);
          } else {
            otherVal = val;
          }
        });
        setSelectedValue(newSelectedValue);
        setOtherValue(otherVal);
        if (otherVal !== undefined) setOtherChecked(true);
      }
    },
    [value],
  );

  /** 变更可选项 */
  var changeSelectedValue = useCallback(
    function (vals) {
      // @todo 校验 value 值类型与 mode 匹配
      if (Array.isArray(vals)) {
        setSelectedValue(vals);
        if (otherValue !== undefined) {
          onChange === null || onChange === void 0
            ? void 0
            : onChange([].concat(_toConsumableArray(vals), [otherValue]));
        } else {
          onChange === null || onChange === void 0 ? void 0 : onChange(_toConsumableArray(vals));
        }
      } else {
        setSelectedValue([vals]);
        onChange === null || onChange === void 0 ? void 0 : onChange(vals);
        setOtherValue(undefined);
        setOtherChecked(false);
      }
    },
    [otherValue, onChange],
  );

  /** 变更其他选项选中后的值 */
  var changeOtherValue = useCallback(
    function (val) {
      setOtherValue(val);
      if (mode === 'multiple') {
        onChange === null || onChange === void 0
          ? void 0
          : onChange(
              val !== undefined
                ? [].concat(_toConsumableArray(selectedValue), [val])
                : selectedValue,
            );
      } else {
        onChange === null || onChange === void 0 ? void 0 : onChange(val);
      }
    },
    [selectedValue, mode, onChange],
  );

  /** 变更其他选项 */
  var changeOtherChecked = useCallback(
    function (checked) {
      setOtherChecked(checked);
      if (!checked) {
        changeOtherValue();
      } else {
        if (mode !== 'multiple') setSelectedValue([]);
      }
    },
    [changeOtherValue],
  );
  return {
    selectedValue: selectedValue,
    otherValue: otherValue,
    otherChecked: otherChecked,
    changeSelectedValue: changeSelectedValue,
    changeOtherValue: changeOtherValue,
    changeOtherChecked: changeOtherChecked,
  };
}
